package org.persistent.impl;

import org.persistent.DBUtil;
import org.persistent.SearchAjaxDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class SearchAjaxDAOImpl implements SearchAjaxDAO {
    private static final String GET_PRODUCTLIST_BY_NAME_STRING = "SELECT name FROM product WHERE name LIKE ?";
    public List<String> findPetsAjax(String name) {
        System.out.println("!"+name);
        name = "%"+name+"%";
        List<String> productList = new ArrayList<String>();
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_PRODUCTLIST_BY_NAME_STRING);
            preparedStatement.setString(1,name);
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next())
            {
                String product = resultSet.getString(1);
                System.out.println(product);
                productList.add(product);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return productList;
    }
}
